package com.park.controller;

import com.park.common.bean.MemberInputView;
import com.park.common.bean.PageBean;
import com.park.common.bean.ResponseBean;
import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.UserOperator;
import com.park.common.util.JsonUtils;
import com.park.platform.service.IMemberService;
import com.park.platform.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("member")
public class MemberController extends BaseController {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IOperatorService operatorService;

    @RequestMapping(value = "memberList")
    public String getUserMembers(MemberInputView memberInputView, Model model) {
        try {
            model.addAllAttributes(JsonUtils.fromJsonDF(memberInputView));
            memberInputView.setRoleId(IDBConstant.ROLE_COMMON);
            PageBean pageBean = memberService.getUserMembers(memberInputView);
            super.setPageInfo(model, pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Members/MembersList";
    }

    @RequestMapping(value = "getSuperior")
    public String getSuperior(int uid, Model model){
        try {
            UserOperator userOperator = operatorService.getUserOperator(uid);
            if(userOperator != null) {
                List<UserOperator> superior = operatorService.getSuperior(uid);
                Map<String, List<UserOperator>> subordinate = operatorService.getSubordinate(uid);
                List<UserOperator> userOperatorList = operatorService.getUserOperatorListByFenxiao();
                model.addAttribute("member", userOperator);
                model.addAttribute("superior", superior);
                model.addAttribute("subordinate", subordinate);
                model.addAttribute("userOperatorList", userOperatorList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Members/MemberFenxiao";
    }

    @RequestMapping(value = "getUserOperatorListByFenxiao")
    public String getUserOperatorListByFenxiao(int uid, Model model){
        try {
            List<UserOperator> userOperatorList = operatorService.getUserOperatorListByFenxiao();
            model.addAttribute("userOperatorList", userOperatorList);
            model.addAttribute("uid", uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Members/Fenxiao_child";
    }

    @ResponseBody
    @RequestMapping("updateUserFenxiao")
    public ResponseBean updateUserFenxiao(int uid, int subUid){
        try{
            if(uid == subUid) throw new MessageException("不能添加自己为下级!");
            operatorService.updateUserFenxiao(uid, subUid);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("deleteUserFenxiao")
    public ResponseBean deleteUserFenxiao(int uid, int subUid){
        try{
            operatorService.deleteUserFenxiao(uid, subUid);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("newCommission")
    public ResponseBean newCommission(int uid, double commission){
        try{
            operatorService.newCommission(uid, commission);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping(value = "memberAdminList")
    public String getUserAdminMembers(MemberInputView memberInputView, Model model) {
        try {
            model.addAllAttributes(JsonUtils.fromJsonDF(memberInputView));
            memberInputView.setRoleId(IDBConstant.ROLE_ADMIN);
            PageBean pageBean = memberService.getUserMembers(memberInputView);
            super.setPageInfo(model, pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Members/AdminMembersList";
    }

    @ResponseBody
    @RequestMapping("addAdminOperator")
    public ResponseBean addAdminOperator(UserOperator userOperator){
        try {
            operatorService.addAdminUserOperator(userOperator);
            return new ResponseBean(true);
        } catch (MessageException e){
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("deleteAdminOperator")
    public ResponseBean deleteAdminOperator(int id){
        try {
            operatorService.deleteAdminUserOperator(id);
            return new ResponseBean(true);
        } catch (MessageException e){
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @RequestMapping(value = "getAdminSuperior")
    public String getAdminSuperior(Integer uid, Model model){
        try {
            if(uid != null) {
                UserOperator userOperator = operatorService.getUserOperator(uid);
                model.addAttribute("member", userOperator);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Members/AdminMembersViews";
    }

}
